using System;
using System.Linq;
using Godot;

namespace VectorSurvivors;

public partial class GameState : Node2D
{
	private Node _currentStateNode;

	public override void _Ready()
	{
		if (GetChildCount() > 1) throw new InvalidOperationException($"NodeContainer must contain only 1 child, but has '{GetChildCount()}' children.");
		if (GetChildCount() == 1)
		{
			_currentStateNode?.QueueFree();
			_currentStateNode = GetChildren().First();
		}
	}

	public void ChangeStateTo(Node newStoredNode)
	{
		_currentStateNode?.QueueFree();
		_currentStateNode = newStoredNode;
		AddChild(newStoredNode);
	}

	public void ClearState()
	{
		_currentStateNode?.QueueFree();
		_currentStateNode = null;
	}

	public Node GetCurrentState()
	{
		return _currentStateNode;
	}
}